package com.qf.homeproject;

import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qf.adapter.AbsAdapter;
import com.qf.bean.NewHome;
import com.qf.bean.NewHomeAll;
import com.qf.request.DownLoadNetHomeData;

public class NewHomeActivity extends Activity
{

	private TextView field, type, price;
	private ListView leftView, rightView;

	private PopupWindow popupWindow;

	private View popupView;

	Handler handler;

	AbsAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_new_home);
		initView();
		initHandler();
		loadData();
	}

	private void initView()
	{
		field = (TextView) findViewById(R.id.field);

		// 加载popupWindow的布局文件
		popupView = getLayoutInflater().inflate(R.layout.popwindow, null);
		leftView = (ListView) popupView.findViewById(R.id.leftlistview);
		rightView = (ListView) popupView.findViewById(R.id.rightlistview);

		leftView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT,
				600, false);

		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

		// 点击空白处让popupWindow消失
		popupWindow.setTouchable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(),
				(Bitmap) null));

		fieldEvent();
	}

	private void fieldEvent()
	{

		field.setOnTouchListener(new OnTouchListener()
		{

			@Override
			public boolean onTouch(View v, MotionEvent event)
			{
				switch (event.getAction())
				{
					case MotionEvent.ACTION_DOWN:

						if (!popupWindow.isShowing())
						{
							Log.i("info", "show函数正在执行");
							popupWindow.showAsDropDown(field, 0, 0);

						}
						else
						{
							Log.i("info", "dismiss函数正在执行");
							popupWindow.dismiss();
						}
						break;

					default:
						break;
				}
				return false;
			}
		});
	}

	private void initHandler()
	{
		if (handler == null)
		{
			handler = new Handler()
			{
				@Override
				public void handleMessage(Message msg)
				{
					if (msg.what == 0)
					{

						List<NewHomeAll> datas = (List<NewHomeAll>) msg.obj;
						Log.i("info", "下载下来的数据为:" + datas);
						initDataLeft(datas);
						initRightListView(datas);
					}
				}
			};
		}
	}

	private void initDataLeft(List<NewHomeAll> datas)
	{
		adapter = new AbsAdapter<NewHomeAll>(getApplicationContext(),
				R.layout.item_newhome, datas)
		{
			@Override
			public void showData(com.qf.adapter.AbsAdapter.ViewHolder vHolder,
					NewHomeAll data)
			{

				TextView tvInfo = (TextView) vHolder.getView(R.id.tvnewhomeid);
				tvInfo.setText(data.getGc_name());

			}
		};
		leftView.setAdapter(adapter);
		List<NewHome> subList = datas.get(0).getSubList();
		initDataRight(subList);
	}

	private void loadData()
	{

		DownLoadNetHomeData.loadDataFromServer(handler);
	}

	private void initRightListView(final List<NewHomeAll> datas)
	{
		leftView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				List<NewHome> subList = datas.get(position).getSubList();
				initDataRight(subList);
			}
		});
	}

	private void initDataRight(List<NewHome> datas)
	{
		adapter = new AbsAdapter<NewHome>(getApplicationContext(),
				R.layout.item_newhomeright, datas)
		{
			@Override
			public void showData(com.qf.adapter.AbsAdapter.ViewHolder vHolder,
					NewHome data)
			{
				TextView tvInfo = (TextView) vHolder.getView(R.id.tvnewhomeid);
				tvInfo.setText(data.getGc_name());

			}
		};
		rightView.setAdapter(adapter);
	}

}
